package com.arsn.myf.generate.util;

import com.arsn.myf.common.constant.Constants;
import com.arsn.myf.generate.entity.GenerateCodeTable;
import com.arsn.myf.generate.entity.TableColumnInfo;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.arsn.myf.common.util.StringUtils.decapitalize;

/**
 * 模板处理工具类
 *
 * @author zhongrj
 * @date 2022-12-06
 */
public class VelocityUtil {

    /**
     * 初始化vm方法
     */
    public static void initVelocity() {
        Properties properties = new Properties();
        try {
            // 加载classpath目录下的vm文件
            properties.setProperty("file.resource.loader.class",
                    "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            // 定义字符集
            properties.setProperty(Velocity.ENCODING_DEFAULT, Constants.UTF8);
            properties.setProperty(Velocity.OUTPUT_ENCODING, Constants.UTF8);
            // 初始化Velocity引擎，指定配置Properties
            Velocity.init(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 设置 velocity 上下文
     * @param generateCodeTable
     * @param tableColumnInfoList
     * @return 模板列表
     */
    public static VelocityContext setVelocityContext(GenerateCodeTable generateCodeTable, List<TableColumnInfo> tableColumnInfoList) {
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        //设置上下文信息
        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("tableName", generateCodeTable.getTableName());
        velocityContext.put("functionName", generateCodeTable.getTableComment());
        velocityContext.put("ClassName", generateCodeTable.getEntityName());
        velocityContext.put("className", decapitalize(generateCodeTable.getEntityName()));
        velocityContext.put("packageName", generateCodeTable.getPackageName());
        velocityContext.put("moduleName", generateCodeTable.getModuleName());
        velocityContext.put("author", generateCodeTable.getAuthor());
        velocityContext.put("datetime", dateTime);
        velocityContext.put("columns", tableColumnInfoList);
        //返回
        return velocityContext;
    }

    /**
     * 获取模板信息
     * @return 模板列表
     */
    public static List<String> getTemplateList() {
        List<String> templates = new ArrayList<>();
        templates.add("generate/template/java/controller/controller.java.vm");
        templates.add("generate/template/java/entity/entity.java.vm");
        templates.add("generate/template/java/service/service.java.vm");
        templates.add("generate/template/java/service/serviceImpl.java.vm");
        templates.add("generate/template/java/mapper/mapper.java.vm");
        templates.add("generate/template/java/mapper/mapper.xml.vm");
        templates.add("generate/template/js/api.js.vm");
        templates.add("generate/template/vue/index.vue.vm");
        //返回
        return templates;
    }
}
